﻿/***
    Copyright 2012 Stijn Van Antwerpen
  
    This file is part of Qasparov.

    Qasparov is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Qasparov is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Qasparov. If not, see <http://www.gnu.org/licenses/>.

 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.Oxilly.Qasparov.QsbClient;
using LinqToTwitter;
using System.Diagnostics;

namespace Com.Oxilly.Qasparov.TwitterExtensionConfigurator
{
    class Program
    {
        static void Main(string[] args)
        {
            var client = ClientFactory.CreateClientApi();
                 
            var twitterCtx = new TwitterContext();
            var auth = Authorize();

            if (auth.IsAuthorized)
            {
                Console.WriteLine("Sending credentinals to Qsb");
                client.ProcessMessage(new TwitterExtensionConfigurationMessage
                {
                    ConsumerKey = auth.Credentials.ConsumerKey,
                    ConsumerSecret = auth.Credentials.ConsumerSecret,
                    OAuthToken = auth.Credentials.OAuthToken,
                    AccessToken = auth.Credentials.AccessToken
                });
            }
            else
            {
                Console.WriteLine("Not Authorized");
            }

            client.ProcessMessage(new TwitterQAppStatusMessage
            {
               MessageSender = "TwitterExtensionConfigurationManager",
               Status = "Hello, Qasparov!"
            });
        

        }

        // This method was originaly found, but altered, in a SampleProject of LinqToTwitter
        // http://linqtotwitter.codeplex.com/ and should be used according it's license
        // http://linqtotwitter.codeplex.com/license
        static PinAuthorizer Authorize()
        {
             //configure the OAuth object
            var auth = new PinAuthorizer
            {
                Credentials = new InMemoryCredentials
                {
                    //The application Key and Secret are stored in an Resource file, not included in the project
                    //Create your own application and use theire values.
                    //See: http://dev.twitter.com/apps for more information
                    ConsumerKey = Properties.Resources.OAuthConsumerKey,
                    ConsumerSecret = Properties.Resources.OAuthConsumerSecret
                },
                UseCompression = true,
                GoToTwitterAuthorization = pageLink => Process.Start(pageLink),
                GetPin = () =>
                {
                    // this executes after user authorizes, which begins with the call to auth.Authorize() below.
                    Console.WriteLine("\nAfter you authorize this application, Twitter will give you a 7-digit PIN Number.\n");
                    Console.Write("Enter the PIN number here: ");
                    return Console.ReadLine();
                }
            };
            auth.Authorize();
            return auth;
        }
    }
}
